package com.linjian.togetherbirthday;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;

import com.linjian.togetherbirthday.activity.SplashActivity;
import com.linjian.togetherbirthday.base.BaseActivity;
import com.linjian.togetherbirthday.fragment.ChangePasswrodFragment;
import com.linjian.togetherbirthday.fragment.ChangePhoneFragment;
import com.linjian.togetherbirthday.fragment.EditFragment;
import com.linjian.togetherbirthday.fragment.FoodFragment;
import com.linjian.togetherbirthday.fragment.FoodInfoFragment;
import com.linjian.togetherbirthday.fragment.HomeFragment;
import com.linjian.togetherbirthday.fragment.MovieInfoFragment;
import com.linjian.togetherbirthday.fragment.MovieListFragment;
import com.linjian.togetherbirthday.fragment.MyDynamicFragment;
import com.linjian.togetherbirthday.fragment.MyOnADateFragment;
import com.linjian.togetherbirthday.fragment.OnADateInfoFragment;
import com.linjian.togetherbirthday.fragment.RegistManagerFragment;
import com.linjian.togetherbirthday.fragment.ReleaseDateFragment;
import com.linjian.togetherbirthday.fragment.ReleaseDynamicFragment;
import com.linjian.togetherbirthday.fragment.SetFragment;
import com.linjian.togetherbirthday.util.StatusBarCompat;
import com.yuntongxun.plugin.common.AppMgr;
import com.yuntongxun.plugin.common.SDKCoreHelper;
import com.yuntongxun.plugin.common.common.utils.RongXInUtils;
import com.yuntongxun.plugin.common.common.utils.ToastUtil;
import com.yuntongxun.plugin.im.manager.IMPluginManager;

public class MainActivity extends BaseActivity {

    private HomeFragment homeFragment;
    private EditFragment editFragment;
    private SetFragment setFragment;
    private ChangePasswrodFragment changePasswrodFragment;
    private ChangePhoneFragment changePhoneFragment;
    private ReleaseDateFragment releaseDateFragment;
    private FoodFragment foodFragment;
    private MyDynamicFragment myDynamicFragment;
    private ReleaseDynamicFragment releaseDynamicFragment;
    private MyOnADateFragment myOnADateFragment;
    private MovieInfoFragment movieInfoFragment;
    private MovieListFragment movieListFragment;
    private FoodInfoFragment foodInfoFragment;
    private OnADateInfoFragment onADateInfoFragment;
    private RegistManagerFragment registManagerFragment;

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    private Context mContext = this;

    private BroadcastReceiver mSDKNotifyReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (RongXInUtils.ACTION_KICK_OFF.equals(intent.getAction())) {
                //代码示例
                ToastUtil.showMessage("您的账号被他人登陆，请确定您的账号安全");
                startActivity(new Intent(MainActivity.this, SplashActivity.class));
                finish();
            }
        }
    };

    @Override
    protected void initView() {
        setContentView(R.layout.activity_main);
        StatusBarCompat.translucentStatusBar(this);

        String str = AppMgr.getClientUser() != null ? AppMgr.getClientUser().getUserName() : " 无名氏";
        NotifyIntent(getIntent());

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(SDKCoreHelper.ACTION_SDK_CONNECT);// SDK状态广播
        intentFilter.addAction(RongXInUtils.ACTION_KICK_OFF);// 账号异地登入广播
        registerReceiver(mSDKNotifyReceiver, intentFilter);
    }

    public static String getNameById(String id) {
        return "名字" + id;
    }

    @Override
    protected void initData() {
        homeFragment = HomeFragment.newInstance();
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frame_layout, homeFragment)
                .commit();


    }

    @Override
    protected void setEvent() {

    }

    public void replaceFragment(String frag,String data) {

        transaction = getSupportFragmentManager().beginTransaction();
//        transaction.setCustomAnimations(R.anim.enter_scale,R.anim.exit_scale);
        switch (frag) {
            case "HomeFragment":
                homeFragment = HomeFragment.newInstance();
                transaction.replace(R.id.frame_layout, homeFragment);
                break;
            case "EditFragment":
                editFragment = EditFragment.newInstance();
                transaction.replace(R.id.frame_layout, editFragment);
                transaction.addToBackStack("EditFragment");
                break;
            case "SetFragment":
                setFragment = SetFragment.newInstance();
                transaction.replace(R.id.frame_layout, setFragment);
                transaction.addToBackStack("SetFragment");
                break;
            case "ChangePasswrodFragment":
                changePasswrodFragment = ChangePasswrodFragment.newInstance();
                transaction.replace(R.id.frame_layout, changePasswrodFragment);
                transaction.addToBackStack("ChangePasswrodFragment");
                break;
            case "ChangePhoneFragment":
                changePhoneFragment = ChangePhoneFragment.newInstance();
                transaction.replace(R.id.frame_layout, changePhoneFragment);
                transaction.addToBackStack("ChangePhoneFragment");
                break;
            case "ReleaseDateFragment":
                releaseDateFragment = ReleaseDateFragment.newInstance(data);
                transaction.replace(R.id.frame_layout, releaseDateFragment);
                transaction.addToBackStack("ReleaseDateFragment");
                break;
            case "FoodFragment":
                foodFragment = FoodFragment.newInstance();
                transaction.replace(R.id.frame_layout, foodFragment);
                transaction.addToBackStack("FoodFragment");
                break;
            case "MyDynamicFragment":
                myDynamicFragment = MyDynamicFragment.newInstance();
                transaction.replace(R.id.frame_layout, myDynamicFragment);
                transaction.addToBackStack("MyDynamicFragment");
                break;
            case "ReleaseDynamicFragment":
                releaseDynamicFragment = ReleaseDynamicFragment.newInstance();
                transaction.replace(R.id.frame_layout,releaseDynamicFragment);
                transaction.addToBackStack("ReleaseDynamicFragment");
                break;
            case "MyOnADateFragment":
                myOnADateFragment = MyOnADateFragment.newInstance();
                transaction.replace(R.id.frame_layout,myOnADateFragment);
                transaction.addToBackStack("MyOnADateFragment");
                break;
            case "MovieInfoFragment":
                movieInfoFragment = MovieInfoFragment.newInstance();
                transaction.replace(R.id.frame_layout,movieInfoFragment);
                transaction.addToBackStack("MovieInfoFragment");
                break;
            case "MovieListFragment":
                movieListFragment = MovieListFragment.newInstance();
                transaction.replace(R.id.frame_layout,movieListFragment);
                transaction.addToBackStack("MovieListFragment");
                break;
            case "FoodInfoFragment":
                foodInfoFragment = FoodInfoFragment.newInstance(data);
                transaction.replace(R.id.frame_layout,foodInfoFragment);
                transaction.addToBackStack("FoodInfoFragment");
                break;
            case "OnADateInfoFragment":
                onADateInfoFragment = OnADateInfoFragment.newInstance(data);
                transaction.replace(R.id.frame_layout,onADateInfoFragment);
                transaction.addToBackStack("OnADateInfoFragment");
                break;
            case "RegistManagerFragment":
                registManagerFragment = RegistManagerFragment.newInstance(data);
                transaction.replace(R.id.frame_layout,registManagerFragment);
                transaction.addToBackStack("RegistManagerFragment");
                break;
            default:
                homeFragment = HomeFragment.newInstance();
                transaction.replace(R.id.frame_layout, homeFragment);
                break;
        }

        transaction.commit();
    }


    @Override
    public void onBackPressed() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mSDKNotifyReceiver != null) {
            // 注销广播
            unregisterReceiver(mSDKNotifyReceiver);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        NotifyIntent(intent);
    }

    private void NotifyIntent(Intent intent) {
        String contactId = intent.getStringExtra("contactId");
        if (TextUtils.isEmpty(contactId)) {
            return;
        }
        // 获取到contactID后调用该方法，方法为自动判断是单聊还是群聊
        IMPluginManager.getManager().startChatting(mContext, contactId);
    }

}
